﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;

namespace JsAndSilverLight
{
    /// <summary>
    /// 地图放大变小滑动条
    /// </summary>
    public partial class MapSilder : UserControl, System.ComponentModel.INotifyPropertyChanged
    {
        public MapSilder()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 地图
        /// </summary>
        private ESRI.ArcGIS.Client.Map map = new ESRI.ArcGIS.Client.Map();

        /// <summary>
        /// 注册事件
        /// </summary>
        public static readonly DependencyProperty MapProperty = DependencyProperty.Register("Map", typeof(ESRI.ArcGIS.Client.Map), typeof(MapSilder),
            new PropertyMetadata(new PropertyChangedCallback(
                (DependencyObjectobj, Nobj) =>
                {
                    //设定值
                    (DependencyObjectobj as MapSilder).map = Nobj.NewValue as Map ;
                })));


        /// <summary>
        ///得到或设置 地图
        /// </summary>
        public ESRI.ArcGIS.Client.Map Map
        {
            get {
                return GetValue(MapProperty) as ESRI.ArcGIS.Client.Map;
            }
            set
            {
                SetValue(MapProperty, value);

                map = value;
               

                if (PropertyChanged != null)
                {
                    PropertyChanged(this,new System.ComponentModel.PropertyChangedEventArgs("Map"));
                }
                if (map != null)

                {

                    map.ExtentChanged += new EventHandler<ExtentEventArgs>(map_ExtentChanged);

                    map.SnapToLevels = true;

                    ((ArcGISTiledMapServiceLayer)Map.Layers[0]).Initialized += new EventHandler<EventArgs>(layer0_initialized);

                }


            }
        }

                private void layer0_initialized(object o,EventArgs e)

        {

            sliderLOD.Maximum = ((ArcGISTiledMapServiceLayer)Map.Layers[0]).TileInfo.Lods.Length - 1;

        }





        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

        {

            if (map!=null)

            {

                Map.ZoomToResolution(((ArcGISTiledMapServiceLayer)Map.Layers[0]).TileInfo.Lods[Convert.ToInt32(e.NewValue)].Resolution);

            }

        }



        private void map_ExtentChanged(object o, ExtentEventArgs e)

        {

            ArcGISTiledMapServiceLayer layer = Map.Layers[0] as ArcGISTiledMapServiceLayer;

            int i;

            for (i = 0; i < layer.TileInfo.Lods.Length; i++)

            {

                if (Map.Resolution == layer.TileInfo.Lods[i].Resolution)

                    break;

            }



            sliderLOD.Value = i;

        }



        private void btnzoomin_Click(object sender, RoutedEventArgs e)

        {

            sliderLOD.Value += 1;

        }



        private void btnzoomout_Click(object sender, RoutedEventArgs e)

        {

            sliderLOD.Value -= 1;

        }

        #region INotifyPropertyChanged 成员

        /// <summary>
        /// 当属性值改变时的事件
        /// </summary>
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        #endregion

    }
}
